﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TBlog.Models;
using TBlog.Models.ViewData;
using TBlog.Helpers;

namespace TBlog.Models
{
    public class TBlogRepository : ITBlogRepository
    {
        // 
        private TBlogEntities _entities = new TBlogEntities();

        //
        public IEnumerable<Category> ListCategory()
        {
            return _entities.Category.ToList();            
        }

        //
        public Category CreateCategory(Category categoryToCreate)
        {
            _entities.AddToCategory(categoryToCreate);
            _entities.SaveChanges();
            return categoryToCreate;
        }

        //
        public Category GetCategory(int? categoy_id)
        {
            return (from c in _entities.Category
                    where c.category_id == categoy_id
                    select c).FirstOrDefault();

                    //select new { categoy_id = c.category_id, category_name = c.category_name, categoryDescription = c.category_description, CategoryNumPost = c.Posts.Count() });

            //ObjectDumper.Write(a);

            //return a.FirstOrDefault();
            //return _entities.Category.FirstOrDefault();
        }

        //
        public void DeleteCategory(Category categoryToDelete)
        {
            var originalCategoryToDelete = GetCategory(categoryToDelete.category_id);
            _entities.DeleteObject(originalCategoryToDelete);
            _entities.SaveChanges();
        }

        //
        public Category GetFirstCategory()
        {
            return _entities.Category.Include("Category").FirstOrDefault();
        }

        public IEnumerable<Post> ListPost()
        {
            return _entities.Post.ToList();
        }

        public IEnumerable<Post> ListPost(int categoy_id)
        {
            return (
                    from p in _entities.Post
                    where p.post_category_id == categoy_id
                    select p
                   ).ToList();
        }

        public IEnumerable<Post> GetPostCategory(int categoy_id)
        {
            return _entities.Post.ToList();
        }

        public Post GetPost(int? postId)
        {
            return (from p in _entities.Post
                    where p.post_id == postId
                    select p).FirstOrDefault();
        }

        public IEnumerable<Post> GetFirstPost()
        {
            return (
                    from p in _entities.Post
                    select p
                   ).ToList();
        }

        public Post CreatePost(Post postToCreate)
        {
            _entities.AddToPost(postToCreate);
            _entities.SaveChanges();
            return postToCreate;
        }

        public void DeletePost(Post postToDelete)
        {
            var originalPost = GetPost(postToDelete.post_id);
            _entities.DeleteObject(originalPost);
            _entities.SaveChanges();
        }
        
        public Category EditCategory(Category categoryToEdit)
        {
            var oriCategory = GetCategory(categoryToEdit.category_id);

            _entities.ApplyCurrentValues(oriCategory.EntityKey.EntitySetName, categoryToEdit);
            _entities.SaveChanges();
            return categoryToEdit;
        }

        public Post EditPost(Post postToEdit)
        {
            var oriPost = GetPost(postToEdit.post_id);

            _entities.ApplyCurrentValues(oriPost.EntityKey.EntitySetName, postToEdit);
            _entities.SaveChanges();
            return postToEdit;
        }

        public IQueryable<Post> ListPost(string postType)
        {
            return (from p in _entities.Post
                    where p.post_type == postType
                    select p).AsQueryable();
        }

    }
}